<template>
  <el-form ref="myInfoForm"
           :model="myInfo"
           :rules="rules"
           :inline="true">
    <el-form-item label="真实姓名"
                  label-width="100px"
                  prop="realname">
      <el-input v-model="myInfo.realname"
                maxlength="10"
                type="text" />
    </el-form-item>

    <el-form-item label="手机号码"
                  label-width="100px"
                  prop="phone">
      <el-input v-model="myInfo.phone"
                maxlength="11"
                type="text" />
    </el-form-item>
    <el-form-item>
      <el-button :loading="loading"
                 type="primary"
                 @click.native.prevent="submit">修改</el-button>
    </el-form-item>
    <br>
    <el-form-item label="所属公司"
                  label-width="100px">
      <el-input v-model="myInfo.company.name"
                type="text"
                disabled />
    </el-form-item>
    <el-form-item label="所属部门"
                  label-width="100px">
      <el-input v-model="myInfo.department.name"
                type="text"
                disabled />
    </el-form-item>
    <br>
    <el-form-item label="角色名称"
                  label-width="100px">
      <el-input v-model="myInfo.role.name"
                type="text"
                disabled />
    </el-form-item>
    <el-form-item label="所属级别"
                  label-width="100px">
      <el-input v-model="myInfo.levelName"
                type="text"
                disabled />
    </el-form-item>
  </el-form>
</template>

<script>
import { getMyInfo, updateMyInfo } from '@/api/admin'

export default {
  data() {
    return {
      myInfo: {
        realname: '',
        phone: '',
        company: {},
        department: {},
        role: {},
        levelName: ''
      },
      rules: {
        realname: [{ required: true, message: '请输入真实姓名', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入手机号码', trigger: 'blur' }]
      },
      loading: false
    }
  },
  created() {
    this.getMyInfo()
  },
  methods: {
    async getMyInfo() {
      this.myInfo = await getMyInfo()
    },
    submit() {
      this.$refs.myInfoForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/changeRealname', { realname: this.myInfo.realname, phone: this.myInfo.phone })
            .then(() => {
              this.$message({
                message: '修改成功',
                type: 'success',
                duration: 3 * 1000
              })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>
